package com.xbongbong.saas.script.tempenum;/*/*/

import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.WorkOrderEmergencyEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;
import com.xbongbong.saas.enums.dictionary.WorkOrderShipStatusEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * 工单模板
 *
 * @author 魏荣杰
 * @version v1.0
 * @date 2019/3/12 17:24
 * @since v1.0
 */
@Getter
public enum TempWorkOrderEnum {
    /**
     * 工单模板设置展示字段
     */
    SHEET_NO(WorkOrderEnum.SHEET_NO),
    NAME(WorkOrderEnum.NAME),
    LINK_CUSTOMER(WorkOrderEnum.LINK_CUSTOMER),
    LINK_CONTACT(WorkOrderEnum.LINK_CONTACT),
    LINK_OPPORTUNITY(WorkOrderEnum.LINK_OPPORTUNITY),
    LINK_CONTRACT(WorkOrderEnum.LINK_CONTRACT),
    CONTACT_INFORMATION(WorkOrderEnum.CONTACT_INFORMATION),
    SPARE_PARTS(WorkOrderEnum.SPARE_PARTS),
    SHIP_STATUS(WorkOrderEnum.SHIP_STATUS),
    ADDRESS(WorkOrderEnum.ADDRESS),
    SERVICE_PRICE(WorkOrderEnum.SERVICE_PRICE),
    IMPLEMENTATION_TIME(WorkOrderEnum.IMPLEMENTATION_TIME),
    MEMO(WorkOrderEnum.MEMO),
    EMERGENCY_LEVEL(WorkOrderEnum.EMERGENCY_LEVEL),
    LINK_CUSTOMER_HIDE(WorkOrderEnum.LINK_CUSTOMER_HIDE),
    LINK_CONTACT_HIDE(WorkOrderEnum.LINK_CONTACT_HIDE),
    LINK_OPPORTUNITY_HIDE(WorkOrderEnum.LINK_OPPORTUNITY_HIDE),
    LINK_CONTRACT_HIDE(WorkOrderEnum.LINK_CONTRACT_HIDE),
    MANAGER_ID(WorkOrderEnum.MANAGER_ID),
    TIME_OUT(WorkOrderEnum.TIME_OUT),
    STATUS(WorkOrderEnum.STATUS),
    STAGE(WorkOrderEnum.STAGE),
    OWNER_ID(WorkOrderEnum.OWNER_ID),
    COUSERID(WorkOrderEnum.COUSERID),
    CREATOR_ID(WorkOrderEnum.CREATOR_ID),
    ADD_TIME(WorkOrderEnum.ADD_TIME),
    UPDATE_TIME(WorkOrderEnum.UPDATE_TIME),
    COMPLETE_TIME(WorkOrderEnum.COMPLETE_TIME),
    CLOSE_TIME(WorkOrderEnum.CLOSE_TIME),
    RATE_STAR(WorkOrderEnum.RATE_STAR),
    NODE_STATUS(WorkOrderEnum.NODE_STATUS),
    STAGE_NAME(WorkOrderEnum.STAGE_NAME),
    ;
    private WorkOrderEnum anEnum;

    TempWorkOrderEnum(WorkOrderEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempWorkOrderEnum> getAllEnum() {
        List<TempWorkOrderEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    public static void initName(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.WORK_ORDER.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.WORK_ORDER.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(NAME.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
    }

    public static void initWorkOrderNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.WORK_ORDER.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.WORK_ORDER.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(SHEET_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        fieldAttrEntity.setSerialNumber(InitExplainsHelper.setSerialNumber(SerialNumberEnum.WORKORDER_NO));
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initLinkCustomer(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CUSTOMER.getCode());
        fieldAttrEntity.setLinkedText(LINK_CUSTOMER_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initLinkContact(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CONTACT.getCode());
        fieldAttrEntity.setLinkedText(LINK_CONTACT_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyAttr(LINK_CUSTOMER.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initLinkOpportunity(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.SALES_OPPORTUNITY.getCode());
        fieldAttrEntity.setLinkedText(LINK_OPPORTUNITY_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyAttr(LINK_CUSTOMER.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initLinkContract(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CONTRACT.getCode());
        fieldAttrEntity.setLinkedText(LINK_CONTRACT_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(true);
        saasParticularAttributePoJo.setRelyAttr(LINK_CUSTOMER.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initContactInformation(FieldAttrEntity fieldAttrEntity) {
        SubFormPoJo subFormPoJo = new SubFormPoJo();
        List<FieldAttrEntity> items = new ArrayList<>();
        FieldAttrEntity item = InitExplainsHelper.setField("text_1", "类型", "type", FieldTypeEnum.COMBO.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
        String str = "工作,手机,住址,主要,传真,其他";
        List<ItemPoJo> itemPoJoList = InitExplainsHelper.setCombItem(str);
        itemPoJoList.get(0).setChecked(true);
        item.setItems(itemPoJoList);
        item.setRequired(1);
        item.setDictionaryFlag(1);
        item.setParentAttr(WorkOrderEnum.CONTACT_INFORMATION.getAttr());
        items.add(item);
        FieldAttrEntity item2 = InitExplainsHelper.setField("text_2", "电话", "phone", FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
        item2.setNoRepeat(1);
        item2.setRequired(1);
        item2.setParentAttr(WorkOrderEnum.CONTACT_INFORMATION.getAttr());
        items.add(item2);
        subFormPoJo.setItems(items);
        fieldAttrEntity.setSubForm(subFormPoJo);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("custom");
        defaultAttrPoJo.setDefaultValue("");
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }

    public static void initSpareParts(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRequired(0);
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initAddress(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType("");
        fieldAttrEntity.setShowDetailAddress(1);
    }

    public static void initServicePrice(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initImplementationTime(FieldAttrEntity fieldAttrEntity) {

    }

    public static void initMemo(FieldAttrEntity fieldAttrEntity) {

    }

    public static void initEmergencyLevel(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(WorkOrderEmergencyEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(0);
        fieldAttrEntity.setIsForbidden(1);
    }

    public static void initTimeOut(FieldAttrEntity fieldAttrEntity) {
        String str = "超时,未超时";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItem(str));
    }

    /**
     * 负责人
     *
     * @param fieldAttrEntity
     */
    public static void initOwnerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setMultiple(1);
    }

    /**
     * 协同人
     *
     * @param fieldAttrEntity
     */
    public static void initCoUserId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setRequired(0);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增协同人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setSetType(3);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }

    /**
     * 出库状态
     *
     * @param fieldAttrEntity
     */
    public static void initShipStatus(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsDictionary(1);
        List<ItemPoJo> itemPoJos = InitExplainsHelper.setCombItemByEnum(WorkOrderShipStatusEnum.class, fieldAttrEntity.getFieldType());
        Iterator<ItemPoJo> iterator = itemPoJos.iterator();
        while (iterator.hasNext()) {
            ItemPoJo itemPoJo = iterator.next();
            // 为空的状态不用刷到解释里
            if (Objects.equals(itemPoJo.getText(), WorkOrderShipStatusEnum.EMPTY.getName())) {
                iterator.remove();
                continue;
            }
            itemPoJo.setNoDel(1);
            itemPoJo.setNoEdit(1);
        }
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_OPTION_RELY.getAlias(), ForbiddenSettingEnum.DISABLE_SELF_DEFINE.getAlias(), ForbiddenSettingEnum.DISABLE_REQUIRE_RULE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setItemBanDefault(1);
        fieldAttrEntity.setItems(itemPoJos);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }
}
